package com.company.cloud.goodsrestservice.web;

import com.company.cloud.goodsrestservice.core.Result;
import com.company.cloud.goodsrestservice.core.ResultGenerator;
import com.company.cloud.goodsrestservice.model.Order;
import com.company.cloud.goodsrestservice.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.List;


/**
* Created by reabout on 2018/03/27.
*/
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @PostMapping("/add")
    public Result add(@RequestBody Order order) {
        Integer oid = orderService.add(order);

        return ResultGenerator.genSuccessResult(oid);
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        orderService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Order order) {
        orderService.update(order);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/get")
    public Result detail(@RequestParam Integer id) {
        Order order = orderService.findById(id);
        if (order != null) {
            return ResultGenerator.genSuccessResult(order);
        } else {
            return ResultGenerator.genFailResult("SKU定单信息不存在");
        }
    }

    @PostMapping("/getBy")
    public Result detail(@RequestBody Order order) {
        List<Order> result = orderService.getBy(order);
        if (result != null & !result.isEmpty()) {
            return ResultGenerator.genSuccessResult(result);
        } else {
            return ResultGenerator.genFailResult("SKU定单信息不存在");
        }
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size, "create_time DESC");
        Condition condition = new Condition(Order.class);
        condition.setOrderByClause(" create_time desc");
        List<Order> list = orderService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
